package com.um.jdy.platform.station.manager;

import cn.hutool.core.date.DateUtil;
import com.um.jdy.common.device.service.StationService;
import com.um.jdy.platform.station.entity.vo.StationMonthItemVO;
import com.um.jdy.platform.station.entity.vo.StationOverviewVO;
import com.um.jdy.platform.station.entity.vo.StationProvinceItemVO;
import com.um.jdy.platform.station.entity.vo.StationTypeItemVO;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.manager.BaseManager;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @author 邓南辉
 * 日期 2023/7/4
 * 版权所有 广州优匠科技有限公司
 */
@Component
public class StationOverviewManager extends BaseManager<StationService> {

    /**
     * 网点概览图表
     * @return
     */
    public Response<StationOverviewVO> getOverviewData(){

        Date date = new Date();
        String comId = getComId();
        String year = DateUtil.format(date,"yyyy");

        StationOverviewVO stationOverviewVO = new StationOverviewVO();

        Integer totalCount = service.getCountBySqlName("get_station_overview_total_count",new Object[]{comId}).intValue();
        Integer monthIncrease = service.getCountBySqlName("get_station_overview_month_increase",new Object[]{DateUtil.format(date,"yyyy-MM"),comId}).intValue();
        Integer yearIncrease = service.getCountBySqlName("get_station_overview_year_increase",new Object[]{year,comId}).intValue();

        List<StationMonthItemVO> stationMonthList = service.getViewModelList("get_station_overview_month_increase_list",StationMonthItemVO.class,new Object[]{year,comId});

        for(int index = 1;index <= 12;index ++){
            String month = year+"-"+(index < 10 ? "0"+index : index);

            boolean hasFind = false;
            for(StationMonthItemVO item : stationMonthList){
                if(item.getMonth().equals(month)){
                    hasFind = true;
                    break;
                }
            }

            if(!hasFind){
                StationMonthItemVO newItem = new StationMonthItemVO();
                newItem.setMonth(month);
                newItem.setNum(0);
                stationMonthList.add(index - 1,newItem);
            }
        }

        List<StationTypeItemVO> stationTypeList = service.getViewModelList("get_station_overview_type_list",StationTypeItemVO.class,new Object[]{comId});
        Integer sum  = stationTypeList.stream().mapToInt(StationTypeItemVO::getNum).sum();
        for(StationTypeItemVO item : stationTypeList){
            item.setRadio(item.getNum() * 1.0 / sum * 100);
        }

        List<StationProvinceItemVO> stationProvinceList = service.getViewModelList("get_station_overview_province_list",StationProvinceItemVO.class,new Object[]{comId});

        stationOverviewVO.setTotalCount(totalCount);
        stationOverviewVO.setMonthIncrease(monthIncrease);
        stationOverviewVO.setYearIncrease(yearIncrease);

        stationOverviewVO.setStationMonthList(stationMonthList);
        stationOverviewVO.setStationTypeList(stationTypeList);
        stationOverviewVO.setStationProvinceList(stationProvinceList);

        return Response.success(stationOverviewVO);
    }
}
